package week2;


import java.util.Scanner;

public class Main7 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
			int n ;
			n = sc.nextInt();
			String[] strs=new String [n];
			for(int i=0;i<n;i++){
				strs[i] = sc.next();
			}
			while(true){
				
			String strs1 = sc.next();
			switch(strs1){
			case "sort1":
				first(strs,n);
				for(int i=0;i<n;i++){
					System.out.println(strs[i].substring(6,10)+"-"+strs[i].substring(10,12)+"-"+strs[i].substring(12,14));
				}
				break;
			case "sort2":
				first(strs,n);
				for(int i=0;i<n;i++){
					System.out.println(strs[i]);
				}
				break;
			default:
				System.out.println("exit");
				System.exit(0);
				break;
		
			}
		}	
}
		
	public static void first(String[] p,int n){
		int x = 0;
		for(int i =0;i<n-1;i++){
			for(int j =0;j<n-1-i;j++){
				x=(p[j].substring(6,14).compareTo(p[j+1].substring(6,14)));
				if(x>0){
					String temp = p[j];
					p[j]=p[j+1];
					p[j+1]=temp;
				}
			}
		}
	}

}
